package com.uchengwang.ui.cm_ui.ucity.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.uchengwang.R;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.ucity.cityBean.ReleaseAddBean;
import com.zhy.autolayout.utils.AutoUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2018/2/1.
 * 发布享说图片展示
 */

public class ReleaseDynamicAdapter extends BaseAdapter {
    private Context context;
    private List<ReleaseAddBean> imageList = new ArrayList<>();

    public ReleaseDynamicAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<ReleaseAddBean> data) {
        this.imageList = data;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return imageList.size();
    }

    @Override
    public Object getItem(int i) {
        return imageList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ReleaseVH vh = null;
        if (view == null) {
            vh = new ReleaseVH();
            view = LayoutInflater.from(context).inflate(R.layout.item_release_speek, null, false);

            vh.image = (ImageView) view.findViewById(R.id.itemSpeek_image);
            vh.itemSpeek_del = (ImageView) view.findViewById(R.id.itemSpeek_del);

            view.setTag(vh);
            AutoUtils.autoSize(view);
        } else {
            vh = (ReleaseVH) view.getTag();
        }
        final ReleaseAddBean bean = imageList.get(i);
        final String url = bean.getFile();
        if (!TextUtils.isEmpty(url)){
            //是图片
            File file = new File(url);
            if (file.exists()) {
                //文件是否存在
                ImageManager.getInstance().loadImage(context, vh.image, url, ImageManager.ImageType.NARMOL);
                vh.itemSpeek_del.setVisibility(View.VISIBLE);
            } else {
                imageList.remove(url);
            }
        }else {
            //是添加图片
            ImageManager.getInstance().loadImage(context, vh.image, bean.getResId(), ImageManager.ImageType.NARMOL);
            vh.itemSpeek_del.setVisibility(View.GONE);
        }

        vh.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(url)){
                    //说明是添加图片

                }
            }
        });
        vh.itemSpeek_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        return view;
    }

    private class ReleaseVH {
        private ImageView image, itemSpeek_del;
    }

//    interface OnAddDelImageListener{
//        void addImage(List<ReleaseAddBean>);
//        void del(String delValue);
//    }
}